package com.iflytek.cosmo.ocr.service.impl;

import com.iflytek.cosmo.ocr.common.constant.AbstractExceptionCodeConstant;
import com.iflytek.cosmo.ocr.common.constant.AbstractExceptionDescribeConstant;
import com.iflytek.cosmo.ocr.common.constant.AbstractVersionUpdateStatusConstants;
import com.iflytek.cosmo.ocr.common.exception.VersionInfoOutputExceptionAbstract;
import com.iflytek.cosmo.ocr.dao.VersionInfoDao;
import com.iflytek.cosmo.ocr.model.domain.VersionInfoOutput;
import com.iflytek.cosmo.ocr.model.entity.VersionInfoEntity;
import com.iflytek.cosmo.ocr.service.VersionInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class VersionInfoServiceImpl implements VersionInfoService {
    /**
     * 日志记录器
     */
    private Logger logger = LoggerFactory.getLogger(VersionInfoServiceImpl.class);

    /**
     * 版本基础信息dao
     */
    @Autowired
    private VersionInfoDao versionInfoDao;

    @Override
    public List<VersionInfoOutput> queryVersionInfoList(String projectName) throws VersionInfoOutputExceptionAbstract {

        VersionInfoEntity condition=new VersionInfoEntity();

        condition.setProjectName(projectName);
        //根据项目名称查询相应客户端版本基础信息
        List<VersionInfoEntity> versionInfoList=versionInfoDao.select(condition);

        if(CollectionUtils.isEmpty(versionInfoList)){
            //获取版本信息失败自定义异常
            logger.error("projectName:{}, 异常VersionInfoOutputException:{}",projectName, AbstractExceptionDescribeConstant.VERSION_INFO_EXCEPTION);
            throw new VersionInfoOutputExceptionAbstract("异常代码:"+ AbstractExceptionCodeConstant.TARGET_NOT_EXIST_EXCEPTION_CODE);
        }

        //版本基础信息输出对象
        List<VersionInfoOutput> versionInfoOutputList=new ArrayList<VersionInfoOutput>();

        for(VersionInfoEntity versionInfoEntity:versionInfoList){
            VersionInfoOutput versionInfoOutput=new VersionInfoOutput();
            versionInfoOutput.setId(0L);
            versionInfoOutput.setAppId(versionInfoEntity.getAppId());
            versionInfoOutput.setName(versionInfoEntity.getProjectProfile());//profile名称
            versionInfoOutput.setCurrentVer(versionInfoEntity.getCurrentVersionNumber());//当前版本号
            versionInfoOutput.setMinVer(versionInfoEntity.getMinVerisonNumber());//最小版本号
            //是否启用  true:启用   false:禁用
            versionInfoOutput.setEnable(AbstractVersionUpdateStatusConstants.ENABLE==versionInfoEntity.getEnableStatus());
            versionInfoOutputList.add(versionInfoOutput);
        }

        return versionInfoOutputList;
    }

}
